package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo12 {
    //实现注册功能,用户名不允许重复
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = sc.nextLine();
        System.out.println("请输入密码:");
        String password = sc.nextLine();
        System.out.println("请输入昵称:");
        String nickname = sc.nextLine();
        try(Connection conn = DBUtils.getConn()) {
            //1.查询用户名是否存在
            String sql = "select id from user where username = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                System.out.println("用户名已经存在");
                return;
            }
            //2.注册:往user表中添加数据
            String insertSql = "insert into user values(null,?,?,?)";
            PreparedStatement insertPs = conn.prepareStatement(insertSql);
            insertPs.setString(1,username);
            insertPs.setString(2,password);
            insertPs.setString(3,nickname);
            //执行插入
            insertPs.executeUpdate();
            System.out.println("注册成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
